package com.lyc.campuslife.controller;

import com.lyc.campuslife.dto.LoginFormDTO;
import com.lyc.campuslife.dto.Result;
import com.lyc.campuslife.dto.UserDTO;
import com.lyc.campuslife.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import static com.lyc.campuslife.utils.HttpRequestConstants.USER_INFO;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lyc
 * @since 2024-05-07
 */
@RestController
@RequestMapping("/user")
@Api(tags="用户接口")
public class UserController {

    @Resource
    private IUserService userService;

    @ApiOperation(value = "获取验证码")
    @PostMapping("code")
    public Result sendCode(@RequestParam("email") @ApiParam("电子邮箱") String email){
        return userService.sendCode(email);
    }

    @PostMapping("/login")
    public Result login(@RequestBody LoginFormDTO loginForm){
        return userService.login(loginForm);
    }

    @GetMapping("/me")
    public Result me(HttpServletRequest request){
        UserDTO user = (UserDTO) request.getAttribute(USER_INFO);
        return Result.ok(user);
    }
    @PostMapping("/sign")
    public Result sign(HttpServletRequest request){
        UserDTO userDTO = (UserDTO)request.getAttribute(USER_INFO);
        Long userId = userDTO.getId();
        return userService.sign(userId);
    }
    @GetMapping("/sign/count")
    public Result signCount(HttpServletRequest request){
        UserDTO userDTO = (UserDTO)request.getAttribute(USER_INFO);
        Long userId = userDTO.getId();
        return userService.signCount(userId);
    }
}
